package com.xlh.service.cs.impl;

import com.google.common.collect.Lists;
import com.xlh.dao.course.CourseBasicMapper;
import com.xlh.dao.cs.CsCourseMapper;
import com.xlh.dao.cs.CsCourseMapperExt;
import com.xlh.dto.cs.CourseSystemCourseDTO;
import com.xlh.dto.cs.CourseSystemDTO;
import com.xlh.enums.course.CourseStatusEnum;
import com.xlh.enums.cs.CourseSystemTypeEnum;
import com.xlh.enums.cs.OperationTypeEnum;
import com.xlh.param.cs.CourseSystemParam;
import com.xlh.pojo.course.CourseBasic;
import com.xlh.pojo.course.CourseBasicExample;
import com.xlh.pojo.cs.CoursePhase;
import com.xlh.pojo.cs.CourseWork;
import com.xlh.pojo.cs.CsCourse;
import com.xlh.pojo.cs.CsCourseExample;
import com.xlh.service.cs.CoursePhaseService;
import com.xlh.service.cs.CourseSystemCourseService;
import com.xlh.service.cs.CourseWorkService;
import com.xlh.util.BeanUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr 2019/10/17
 */
@Service
public class CourseSystemCourseServiceImpl implements CourseSystemCourseService {

    @Autowired
    private CourseBasicMapper courseBasicMapper;
    @Autowired
    private CsCourseMapper mapper;
    @Autowired
    private CsCourseMapperExt mapperExt;
    @Autowired
    private CourseWorkService courseWorkService;
    @Autowired
    private CoursePhaseService coursePhaseService;

    @Override
    public List<CourseSystemCourseDTO> listSystemCourse() {
        // 查询系统课程
        List<CourseBasic> courseList = selectBasicCourse();
        List<Long> courseIds = courseList.stream().map(CourseBasic::getId).collect(Collectors.toList());
        return listCourse(courseIds, courseList, CourseSystemTypeEnum.SYSTEM_COURSE);
    }

    private List<CourseSystemCourseDTO> listCourse(List<Long> courseIds, List courseList, CourseSystemTypeEnum courseTypeEnum) {
        // 查询课程-工作岗位分配情况
        List<CourseWork> courseWorkList = courseWorkService.selectByCourseIds(courseIds, courseTypeEnum.getType());
        List<Long> workCourseIds = courseWorkList.stream().map(CourseWork::getCourseId).collect(Collectors.toList());
        // 查询课程-学习阶段分配情况
        List<CoursePhase> coursePhaseList = coursePhaseService.selectByCourseIds(courseIds, courseTypeEnum.getType());
        List<Long> phaseCourseList = coursePhaseList.stream().map(CoursePhase::getCourseId).collect(Collectors.toList());

        // 设置封装数据
        List<CourseSystemCourseDTO> list = BeanUtil.batchTransform(CourseSystemCourseDTO.class, courseList);
        list.forEach(dto -> {
            dto.setWorkAllotted(workCourseIds.contains(dto.getId()));
            dto.setPhaseAllotted(phaseCourseList.contains(dto.getId()));
            dto.setOperation(OperationTypeEnum.NO_OPERATE.getType());
            dto.setType(courseTypeEnum.getType());
        });

        return list;
    }

    private List<CourseBasic> selectBasicCourse() {
        CourseBasicExample example = new CourseBasicExample();
        example.createCriteria().andDeletedEqualTo(false).andStatusEqualTo(CourseStatusEnum.SHARED.getCode());
        List<CourseBasic> list = courseBasicMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }

    @Override
    public List<CourseSystemCourseDTO> listNotSystemCourse() {
        // 查询非系统课程
        List<CsCourse> courseList = selectCourse();
        List<Long> courseIds = courseList.stream().map(CsCourse::getId).collect(Collectors.toList());
        return listCourse(courseIds, courseList, CourseSystemTypeEnum.NOT_SYSTEM_COURSE);
    }

    @Override
    public void batchEdit(List<CourseSystemParam> insertList, List<CourseSystemParam> updateList, List<CourseSystemParam> deleteList) {
        if (CollectionUtils.isNotEmpty(insertList))
            batchInsert(insertList);
        if (CollectionUtils.isNotEmpty(updateList))
            batchUpdate(updateList);
        if (CollectionUtils.isNotEmpty(deleteList))
            batchDelete(deleteList);
    }

    @Override
    public List<CourseSystemDTO> showNotSystemCourse() {
        List<CsCourse> list = selectCourse();
        return BeanUtil.batchTransform(CourseSystemDTO.class, list);
    }

    private List<CsCourse> selectCourse() {
        CsCourseExample example = new CsCourseExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<CsCourse> list = mapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }

    private void batchDelete(List<CourseSystemParam> deleteList) {
        if (CollectionUtils.isEmpty(deleteList)) {
            return;
        }
        List<Long> ids = deleteList.stream().map(CourseSystemParam::getId).collect(Collectors.toList());

        // 删除非系统课程信息
        CsCourseExample example = new CsCourseExample();
        example.createCriteria().andIdIn(ids);

        CsCourse course = new CsCourse();
        course.setDeleted(true);

        mapper.updateByExampleSelective(course, example);

        // 删除课程-学习阶段与课程-工作岗位中的对应列
        coursePhaseService.deleteByCourseIds(ids);
        courseWorkService.deleteByCourseIds(ids);
    }

    private void batchUpdate(List<CourseSystemParam> updateList) {
        List<CsCourse> list = BeanUtil.batchTransform(CsCourse.class, updateList);
        mapperExt.batchUpdate(list);
    }

    private void batchInsert(List<CourseSystemParam> insertList) {
        List<CsCourse> list = BeanUtil.batchTransform(CsCourse.class, insertList);
        mapperExt.batchInsert(list);
    }
}
